package com.ls.bigdata.servie.Iml;

import com.ls.bigdata.DTO.Page;
import com.ls.bigdata.entity.IPInfo;
import com.ls.bigdata.servie.DataService;
import com.ls.bigdata.utils.PageUtil;
import com.ls.bigdata.utils.Params;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: bit-data
 * @author: lishun
 * @create: 2021-09-23 20:05
 **/
@Service
public class DataServiceImpl implements DataService {
    // 数据源
    private List<IPInfo> list;
    private Map<String,List<IPInfo>> map;
    private String[] company={"联通","电信","移动","其他"};
    /**
     * 加载所有数据
     * @throws IOException
     */
    @PostConstruct
    protected void loadData()  throws IOException {
        File file = new File("src/main/resources/data.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        List<IPInfo> dataList = new ArrayList<>();
        while((data = br.readLine()) != null) {
            String replace = data.replaceAll("\\s{1,}", ",");
            String[] s = replace.split(",", 3);
            String[] strings = s[1].split("\\+", 2);
            try{
                IPInfo node = IPInfo.builder()
                        .ip(StringUtils.trimAllWhitespace(s[0]))
                        .address(strings[1])
                        .counts(strings[0])
                        .company(s[2])
                        .build();
                if (node!=null){
                    dataList.add(node);
                }
            }catch (Exception e){

            }
        }
        br.close();
        isr.close();
        fis.close();
        this.list=dataList;
        loadProvince();
    }

    /**
     * 按省市分类
     * @throws IOException
     */
    protected void loadProvince() throws IOException {
        File file = new File("src/main/resources/province.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        String data = null;
        Map<String, List<IPInfo>> map = new HashMap<>();
        while((data = br.readLine()) != null) {
           map.put(data,new ArrayList<>());
        }
        Set<String> strings = map.keySet();
        this.list.stream().forEach(ipInfo -> {
            for (String string : strings) {
                if (ipInfo.getAddress().contains(string)){
                    map.get(string).add(ipInfo);
                }
            }
        });
        this.map=map;

    }
    @Override
    public Page process(Params params){
        List<IPInfo> searchList = this.list;
        if (Objects.nonNull(params.getKey())){
            // 关键字查询
            List<IPInfo> collect = searchList.stream().filter(ipInfo -> {
                return ipInfo.getAddress().contains(params.getKey());
            }).collect(Collectors.toList());
            searchList=collect;
        }

        return Page.builder()
                .count(searchList.size())
                .records(PageUtil.startPage(searchList,params.getCurrent(),12))
                .build();

    }

     //map里面放数组,数组里面放对象
    @Override
    public Map<String, List<IPInfo>> categoryShow() {

        return map;
    }

    /**
     * 饼图
     * @return
     */
    @Override
    public Map pieChart(){
        Map<String,Integer> map=new HashMap<>();
        for (String s : this.company) {
            map.put(s,0);
        }
        System.out.println(this.list.size());
        Set<String> strings = map.keySet();
        this.list.forEach(ipInfo -> {
            for (String string : strings) {
                if (ipInfo.getCompany().contains(string)){
                    map.put(string,map.get(string)+1);
                    return;
                }
            }
            map.put("其他",map.get("其他")+1);
        });
        return map;
    }
}
